package edu.uoc.tdp.client.pantalles;


import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JLabel;

import edu.uoc.tdp.client.Client;
import edu.uoc.tdp.client.gestors.GestorConsultaEvent;
import edu.uoc.tdp.client.gestors.GestorEscriptori;
import edu.uoc.tdp.client.gestors.GestorEvent;
import edu.uoc.tdp.client.gestors.GestorMantenimient;
import edu.uoc.tdp.client.gestors.ModelTaulaNoEditable;
import edu.uoc.tdp.client.comu.Util;
import edu.uoc.tdp.comu.beans.CentreDocent;
import edu.uoc.tdp.comu.beans.Event;
import edu.uoc.tdp.comu.beans.TipusEvent;
import edu.uoc.tdp.comu.beans.Universitat;
import edu.uoc.tdp.client.gestors.GestorRMI;

import java.awt.Rectangle;
import java.rmi.RemoteException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;

import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;

/**
 * @author Tomeu Antich
 *
 */

public class PantallaConsultaEvent extends JPanel {

	private static final long serialVersionUID = 1L;
	private boolean flagActivaEvent = false;  // 
	
	private Vector<Universitat> UniversitatVector = new Vector<Universitat>(); //
	private Vector<CentreDocent> CentredocentVector = new Vector<CentreDocent>(); //
	private Vector<TipusEvent> TipusEventVector = new Vector<TipusEvent>(); //
	public Event eventSelecionat= null;
	private GestorConsultaEvent gestor = null;
	private JLabel jLabelTitol = null;
	private JLabel jLabelUniversitat = null;
	private JLabel jLabelTipus = null;
	private JLabel jLabelCentre = null;
	private JLabel jLabelDataIniciA = null;
	private JLabel jLabelDataIniciB = null;
	private JLabel jLabelMostrarCancelats = null;
	private JLabel jLabelMostrarRealitzats = null;
	private JTextField jTextFieldDataIniciA = null;
	private JTextField jTextFieldDataIniciB = null;
	private JComboBox jComboBoxUniversitats = null;	
	private JComboBox jComboBoxCentres = null;
	private JComboBox jComboBoxTipusEvent = null;
	private JCheckBox jCheckBoxMostrarCancelats = null;
	private JCheckBox jCheckBoxMostrarRealitzats = null;
	private JButton jButtonCercar = null;
	private JButton jButtonNou = null;
	private JButton jButtonModificar = null;
	private JButton jButtonCancelar = null;
	private JButton jButtonAssistents = null;
	private JButton jButtonVeureEvent = null;
	private JButton jButtonSortir = null;
	private ModelTaulaNoEditable modelTaula = null;
	private String[][] dades = {};
	private String[][] noDades = {};
	private JScrollPane jScrollPane = null;
	private JTable jTable = null;
	
	/**
	 * This is the default constructor
	 */
	public PantallaConsultaEvent() {
		super();
		this.gestor = new GestorConsultaEvent(this);
		modelTaula = new ModelTaulaNoEditable();
		modelTaula.setDataVector(dades, this.gestor.getCapcaleresTaula());
		initialize();
		GestorEvent.CarregaUniversitats(jComboBoxUniversitats, UniversitatVector);
		this.CarregaTipusEvent();
		
		

		

		flagActivaEvent = true;
		

		Universitat univ = UniversitatVector.get(jComboBoxUniversitats.getSelectedIndex());
		GestorEvent.CarregaCentres(univ.getId(), jComboBoxCentres, CentredocentVector);
		
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		jLabelMostrarRealitzats = new JLabel();
		jLabelMostrarRealitzats.setBounds(new Rectangle(307, 140, 120, 16));
		jLabelMostrarRealitzats.setText(Util.GetEtiqueta("Event.consulta.mostrarrealitzats"));
		jLabelMostrarCancelats = new JLabel();
		jLabelMostrarCancelats.setBounds(new Rectangle(47, 139, 120, 16));
		jLabelMostrarCancelats.setText(Util.GetEtiqueta("Event.consulta.mostrarcancelats"));
		jLabelTipus = new JLabel();
		jLabelTipus.setBounds(new Rectangle(420, 105, 100, 16));
		jLabelTipus.setText(Util.GetEtiqueta("Event.consulta.tipus"));
		jLabelDataIniciB = new JLabel();
		jLabelDataIniciB.setBounds(new Rectangle(260, 105, 100, 16));
		jLabelDataIniciB.setText(Util.GetEtiqueta("Event.consulta.datainicib"));
		jLabelDataIniciA = new JLabel();
		jLabelDataIniciA.setBounds(new Rectangle(15, 105, 150, 16));
		jLabelDataIniciA.setText(Util.GetEtiqueta("Event.consulta.datainicia"));
		jLabelCentre = new JLabel();
		jLabelCentre.setBounds(new Rectangle(15, 76, 120, 16));
		jLabelCentre.setText(Util.GetEtiqueta("Event.consulta.centre"));
		jLabelUniversitat = new JLabel();
		jLabelUniversitat.setBounds(new Rectangle(15, 44, 120, 16));
		jLabelUniversitat.setText(Util.GetEtiqueta("Event.consulta.universitat"));
		jLabelTitol = new JLabel();
		jLabelTitol.setFont(new Font("Dialog", Font.BOLD, 16));
		jLabelTitol.setBounds(new Rectangle(15, 15, 200, 20));
		jLabelTitol.setText(Util.GetEtiqueta("Event.consulta.titol"));
		this.setSize(640, 380);
		this.setLayout(null);
		this.add(jLabelTitol, null);
		this.add(jLabelUniversitat, null);
		this.add(getJComboBoxUniversitats(), null);
		this.add(jLabelCentre, null);
		this.add(jLabelTipus, null);
		this.add(getJComboBoxCentres(), null);
		this.add(getJComboBoxTipusEvent(), null);
		this.add(jLabelDataIniciA, null);
		this.add(getJTextFieldDataIniciA(), null);
		this.add(jLabelDataIniciB, null);
		this.add(getJTextFieldDataIniciB(), null);
		this.add(getJCheckBoxMostrarCancelats(), null);
		this.add(getJCheckBoxMostrarRealitzats(), null);
		this.add(jLabelMostrarCancelats, null);
		this.add(jLabelMostrarRealitzats, null);
		this.add(getJButtonCercar(), null);
		this.add(getJButtonNou(), null);
		this.add(getJButtonModificar(), null);
		this.add(getJButtonCancelar(), null);
		this.add(getJButtonAssistents(), null);
		this.add(getJButtonVeureEvent(), null);
		this.add(getJButtonSortir(), null);
		this.add(getJScrollPane(), null);
	}

// ===================comboboxs============================	
	
	/**
	 * This method initializes jComboBoxUniversitats	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBoxUniversitats() {
		if (jComboBoxUniversitats == null) {
			jComboBoxUniversitats = new JComboBox();
			jComboBoxUniversitats.setBounds(new Rectangle(165, 44, 450, 20));
			jComboBoxUniversitats.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					if(flagActivaEvent) {					
						JComboBox index = (JComboBox) e.getSource();
						Universitat univ = UniversitatVector.get(index.getSelectedIndex());
						GestorMantenimient.CargaComboCentresDocents(univ.getId(), jComboBoxCentres, CentredocentVector);
					}
				}
			});

		}
		return jComboBoxUniversitats;
	}

	/**
	 * This method initializes jComboBoxCentres	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBoxCentres() {
		if (jComboBoxCentres == null) {
			jComboBoxCentres = new JComboBox();
			jComboBoxCentres.setBounds(new Rectangle(165, 75, 450, 20));
		}
		return jComboBoxCentres;
	}

	private JComboBox getJComboBoxTipusEvent() {
		if (jComboBoxTipusEvent == null) {
			jComboBoxTipusEvent = new JComboBox();
			jComboBoxTipusEvent.setBounds(new Rectangle(465, 105, 150, 20));
		}
		return jComboBoxTipusEvent;
	}


	
	private void CarregaTipusEvent(){
		try {
			ArrayList<TipusEvent> llista = GestorRMI.rmi.GetTipusEvent();
			for(TipusEvent te : llista) {
				TipusEventVector.add(te);
				jComboBoxTipusEvent.addItem(te.getNom());
			}
			llista = null; 
		}
		catch(Exception ex) {
			MissatgeError(ex.getMessage());
		}
	}
		
	
	/**
	 * This method initializes jTextFieldDataIniciA	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldDataIniciA() {
		if (jTextFieldDataIniciA == null) {
			jTextFieldDataIniciA = new JTextField();
			jTextFieldDataIniciA.setBounds(new Rectangle(165, 106, 70, 18));
		}
		return jTextFieldDataIniciA;
	}

	/**
	 * This method initializes jTextFieldDataIniciB	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldDataIniciB() {
		if (jTextFieldDataIniciB == null) {
			jTextFieldDataIniciB = new JTextField();
			jTextFieldDataIniciB.setBounds(new Rectangle(325, 106, 70, 17));
		}
		return jTextFieldDataIniciB;
	}

	/**
	 * This method initializes jCheckBoxMostrarCancelats	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBoxMostrarCancelats() {
		if (jCheckBoxMostrarCancelats == null) {
			jCheckBoxMostrarCancelats = new JCheckBox();
			jCheckBoxMostrarCancelats.setBounds(new Rectangle(15, 136, 21, 21));
		}
		return jCheckBoxMostrarCancelats;
	}

	/**
	 * This method initializes jCheckBoxMostrarRealitzats	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBoxMostrarRealitzats() {
		if (jCheckBoxMostrarRealitzats == null) {
			jCheckBoxMostrarRealitzats = new JCheckBox();
			jCheckBoxMostrarRealitzats.setBounds(new Rectangle(270, 137, 21, 21));
		}
		return jCheckBoxMostrarRealitzats;
	}

	
// ======================= botons =======	
	/**
	 * This method initializes jButtonCercar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCercar() {
		
		if (jButtonCercar == null) {
			jButtonCercar = new JButton();
			jButtonCercar.setBounds(new Rectangle(510, 168, 100, 25));
			jButtonCercar.setText(Util.GetEtiqueta("Event.consulta.cercar"));
			jButtonCercar.addActionListener(new java.awt.event.ActionListener(){
				public void actionPerformed (
					java.awt.event.ActionEvent e){
					
					
					jScrollPane.setViewportView(getJTable());
					SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
				
					try {
						new GestorConsultaEvent().clickCerca(jComboBoxTipusEvent.getSelectedIndex(),sdf.parse(jTextFieldDataIniciA.getText()),sdf.parse(jTextFieldDataIniciB.getText()), jCheckBoxMostrarCancelats.isSelected(), false);
					} catch (ParseException e1) {
						Util.MissatgeAvis(Util.GetEtiqueta("Errada.dates"));
						e1.printStackTrace();
					}
		
				}
			});
		}
		return jButtonCercar;
	}

	
	
	
	/**
	 * This method initializes jButtonNou	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonNou() {
		if (jButtonNou == null) {
			jButtonNou = new JButton();
			jButtonNou.setBounds(new Rectangle(15, 345, 99, 25));
			jButtonNou.setText(Util.GetEtiqueta("Event.consulta.nou"));
			jButtonNou.addActionListener(new java.awt.event.ActionListener(){
				public void actionPerformed (
					java.awt.event.ActionEvent e){
					GestorEscriptori ge = new GestorEscriptori();			
					new PantallaEscriptori(ge).SetPantallaEvent();		
				
				}
			});
		}
		return jButtonNou;
	}

	/**
	 * This method initializes jButtonModificar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonModificar() {		
		if (jButtonModificar == null) {
			jButtonModificar = new JButton();
			jButtonModificar.setBounds(new Rectangle(115, 345, 99, 25));
			jButtonModificar.setText(Util.GetEtiqueta("Event.consulta.modificar"));
			jButtonModificar.addActionListener(new java.awt.event.ActionListener(){
				public void actionPerformed (java.awt.event.ActionEvent e){
					if (jTable.getSelectedRow() < 0){
						JOptionPane.showMessageDialog(null, Util.GetEtiqueta("Event.consulta.noselecionat"));
					} else{
						GestorEscriptori ge = new GestorEscriptori();  //
						new PantallaEscriptori(ge).SetPantallaEvent(); //
					}			
				}
			});
		}
		return jButtonModificar;
	}

	
	/**
	 * This method initializes jButtonVeureEvent	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonVeureEvent() {
		if (jButtonVeureEvent == null) {
			jButtonVeureEvent = new JButton();
			jButtonVeureEvent.setBounds(new Rectangle(215, 345, 99, 25));
			jButtonVeureEvent.setText(Util.GetEtiqueta("Event.consulta.veureevent"));
			jButtonVeureEvent.addActionListener(new java.awt.event.ActionListener(){
				public void actionPerformed (
					java.awt.event.ActionEvent e){
					if (jTable.getSelectedRow() < 0){
						JOptionPane.showMessageDialog(null, Util.GetEtiqueta("Event.consulta.noselecionat"));
					} else{
						GestorEscriptori ge = new GestorEscriptori(); //
						new PantallaEscriptori(ge).SetPantallaEvent(); //
					}			
				}
			});
		}
		return jButtonVeureEvent;
	}
	
	
	/**
	 * This method initializes jButtonCancelar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	
	
	
	private JButton getJButtonCancelar() {
		if (jButtonCancelar == null) {
			jButtonCancelar = new JButton();
			jButtonCancelar.setBounds(new Rectangle(315, 345, 99, 25));
			jButtonCancelar.setText(Util.GetEtiqueta("Event.consulta.cancelar"));
		}
		return jButtonCancelar;
	}

	/**
	 * This method initializes jButtonAssistents	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonAssistents() {
		if (jButtonAssistents == null) {
			jButtonAssistents = new JButton();
			jButtonAssistents.setBounds(new Rectangle(415, 345, 99, 25));
			jButtonAssistents.setText(Util.GetEtiqueta("Event.consulta.assitents"));
			jButtonAssistents.addActionListener(new java.awt.event.ActionListener(){
				public void actionPerformed (
					java.awt.event.ActionEvent e){
					if (jTable.getSelectedRow() < 0){
						JOptionPane.showMessageDialog(null, Util.GetEtiqueta("Event.consulta.noselecionat"));
					} else{
						// TODO
					}			
				}
			});
			
			
		}
		return jButtonAssistents;
	}

	/**
	 * This method initializes jButtonCancelar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	
	
	
	private JButton getJButtonSortir() {
		if (jButtonSortir == null) {
			jButtonSortir = new JButton();
			jButtonSortir.setBounds(new Rectangle(515, 345, 99, 25));
			jButtonSortir.setText(Util.GetEtiqueta("Event.consulta.sortir"));
			jButtonSortir.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Client.escriptori.DeixarEscriptoriSensePanells();
				}
			});

			
		}
		return jButtonSortir;
	}


	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(16, 205, 600, 124));
			jScrollPane.setViewportView(getJTable());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTable() {
		String[] header = this.gestor.getCapcaleresTaula();
		if (jTable == null) {
			jTable = new JTable(this.dades, header);
			jTable.setEnabled(false);
		}
		return jTable;
	}

	
	
	public void CarregarTaulaLlistaEsdeveniments(String[][] llista){
		dades = llista;
		jTable.setModel(modelTaula);
	}
	
	public void ActualitzaLlistaEvents(String[][] llista, double total){
		dades = llista;
		this.remove(jScrollPane);
		jScrollPane = null;
		jTable = null;
		this.add(getJScrollPane(), null);
	}
	public void ErrorConnexio(){
		JOptionPane.showMessageDialog(null, Util.GetEtiqueta("Event.consulta.ErrorConnexio"), Util.GetEtiqueta("Error"), 0);
		ActualitzaLlistaEvents(noDades,0);
	}
	
	public void NoDades(){
		JOptionPane.showMessageDialog(null, Util.GetEtiqueta("Event.consulta.NoDades"), Util.GetEtiqueta("Avis"), 0);
		ActualitzaLlistaEvents(noDades,0);	
	}
	
	private int getEventSelect(){
		return new GestorConsultaEvent().getArrayIdEvents().get(jTable.getSelectedRow());
	}
	
	/***
	 * @see Carga el combo de universitats
	 */

	
	private void MissatgeError(String msg){
		JOptionPane.showMessageDialog(null, msg, "Error", JOptionPane.ERROR_MESSAGE, null);
	}
	


//	
//	private ArrayList<GestorEvent> GestorEventt() {
//		ArrayList<GestorEvent> llista = new ArrayList<GestorEvent>();
//		
//		GestorEvent q1 = new GestorEvent();
//		q1.setId_event(10);
//		q1.setNom("Base de Dades");
//
//		GestorEvent q2 = new GestorEvent();
//		q2.setId_event(11);
//		q2.setNom("Gastronima Japonesa");
//		
//		GestorEvent q3 = new GestorEvent();
//		q3.setId_event(12);
//		q3.setNom("Societat i cultura");
//		
//		
//
//		
//		llista.add(q1);
//		llista.add(q2);
//		llista.add(q3);
//		
//		return llista;
//	}
}  
